﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using DAO;
using BUS;

namespace AjaxChatService.Controllers
{
    [HttpHeader("Access-Control-Allow-Origin", "*")]
    public class ChatRoomsController : ApiController
    {
        ChatRoomBUS chatRoomBUS = new ChatRoomBUS();

        // POST api/ChatRooms
        public HttpResponseMessage PostChatRoom(ChatRoom chatroom)
        {
            if (ModelState.IsValid)
            {
                chatRoomBUS.AddChatRoom(chatroom);
                RoomMemberBUS roomMemberBUS = new RoomMemberBUS();
                roomMemberBUS.AddRoomMember(new RoomMember
                                            {
                                                UserId = chatroom.HostUserId,
                                                RoomId = chatroom.Id
                                            });

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, chatroom);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = chatroom.Id }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/ChatRooms/5
        public HttpResponseMessage PostDeleteChatRoom(int id)
        {
            if(chatRoomBUS.DeleteChatRoom(id))
                return Request.CreateResponse(HttpStatusCode.OK);

            return Request.CreateResponse(HttpStatusCode.NotFound);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}